import asyncHandler from "express-async-handler";
import Order from "../models/orderModel.js";

//创建订单
const addOrderItems = asyncHandler(async (req, res) => {
  const {
    orderItems,
    shippingAddress,
    paymentMethod,
    itemsPrice,
    shippingPrice,
    totalPrice,
  } = req.body;

  if (orderItems && orderItems.length == 0) {
    res.status(400);
    throw new Error("没有订单信息");
  } else {
    const order = new Order({
      user: req.user._id,
      orderItems,
      shippingAddress,
      paymentMethod,
      itemsPrice,
      shippingPrice,
      totalPrice,
    });

    const createOrder = await order.save();
    res.status(201).json(createOrder);
  }
});

//依据id获取订单
const getOrderById = asyncHandler(async (req, res) => {
  //在查找到的订单后跟上当前登录用户的信息
  const order = await Order.findById(req.params.id).populate(
    "user",
    "name email"
  );
  if (order) {
    res.json(order);
  } else {
    res.status(404);
    throw new Error("查找不到订单");
  }
});

//获取所有订单
const getOrders = asyncHandler(async (req, res) => {
  //连表查询
  const orders = await Order.find({}).populate("user", "id name");
  res.json(orders);
});

//更新支付订单付款状态
const updateOrderToPaid = asyncHandler(async (req, res) => {
  //连表查询
  const order = await Order.findById(req.params.id);
  if (order) {
    order.isPaid = true;
    order.paidAt = Date.now();
    order.paymentResult = {
      id: req.body.id,
      status: req.body.status,
      update_time: req.body.update_time,
      email_address: req.body.email_address,
    };
    const updatedOrder = await order.save();
    res.json(updatedOrder);
  } else {
    res.status(404);
    throw new Error("查找不到订单");
  }
});

//获取登录用户的订单
const getMyOrders = asyncHandler(async (req, res) => {
  //连表查询
  const orders = await Order.find({ user: req.user._id });
  res.json(orders);
});

//更新支付订单发货状态
const updateOrderToDelivered = asyncHandler(async (req, res) => {
  //连表查询
  const order = await Order.findById(req.params.id);
  if (order) {
    order.isDelivered = true;
    order.deliveredAt = Date.now();
    const updatedOrder = await order.save();
    res.json(updatedOrder);
  } else {
    res.status(404);
    throw new Error("查找不到订单");
  }
});

export {
  addOrderItems,
  getOrderById,
  getOrders,
  updateOrderToPaid,
  getMyOrders,
  updateOrderToDelivered,
};
